
* REPLICATION CODE: JENNINGS & WLEZIEN, 'THE COMPARATIVE TIMELINE OF ELECTIONS', AMERICAN JOURNAL OF POLITICAL SCIENCE *

* FIGURE S5. Regression Coefficients for the Polls (US, UK and Germany)

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - U.S., U.K., & GERMANY - VOTE = POLLS *********************
*********************************************************************************************

use MI_COVAR_ALL.dta, clear

gen beta1=.
gen cons=.

gen se_beta1=.
gen se_cons=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrpbetasareg vote_ poll_ if daysbeforeED==`x' & ipoll!=. & imp_!=. & iics_!=. 


matrix bts = e(b_mi)

scalar bt = bts[1,1]
replace beta1=bt in `x'

scalar bt = bts[1,2]
replace cons=bt in `x'


matrix bts = e(V_mi)

scalar bt = bts[1,1]
replace se_beta1=sqrt(bt) in `x'

scalar bt = bts[2,2]
replace se_cons=sqrt(bt) in `x'

ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************

keep beta* se_beta* cons se_cons
gen daysbeforeED=_n

drop if beta1 == .
mi xtset, clear 
sort daysbeforeED

gen beta1_min95=beta1+se_beta1
gen beta1_max95=beta1-se_beta1

gen cons_min95=cons+se_cons
gen cons_max95=cons-se_cons

save MI_BETAS_EQ1.dta, replace

*********************************************************************************************
** MULTIPLE IMPUTATION/BOOTSTRAP - U.S., U.K., & GERMANY - VOTE = POLLS + MP + ECON *********
*********************************************************************************************

use MI_COVAR_ALL.dta, clear

gen beta1=.
gen beta2=.
gen beta3=.

gen cons=.

gen se_beta1=.
gen se_beta2=.
gen se_beta3=.

gen se_cons=.

foreach x of numlist 1/200 {
quietly mi estimate, cmdok: mybstrpbetasareg vote_ poll_ mp_ ics_ if daysbeforeED==`x' & ipoll!=. & imp_!=. & iics_!=. 

matrix bts = e(b_mi)

scalar bt = bts[1,1]
replace beta1=bt in `x'

scalar bt = bts[1,2]
replace beta2=bt in `x'

scalar bt = bts[1,3]
replace beta3=bt in `x'

scalar bt = bts[1,4]
replace cons=bt in `x'

matrix bts = e(V_mi)

scalar bt = bts[1,1]
replace se_beta1=sqrt(bt) in `x'

scalar bt = bts[2,2]
replace se_beta2=sqrt(bt) in `x'

scalar bt = bts[3,3]
replace se_beta3=sqrt(bt) in `x'

scalar bt = bts[4,4]
replace se_cons=sqrt(bt) in `x'

ereturn clear
}

*********************************************************************************************
** OUTPUT/FORMAT THE TIMELINE DATASET *******************************************************
*********************************************************************************************

keep beta* se_beta* cons se_cons
gen daysbeforeED=_n

drop if beta1 == .
mi xtset, clear 
sort daysbeforeED

gen beta1_min95=beta1+se_beta1
gen beta1_max95=beta1-se_beta1

gen beta2_min95=beta2+se_beta2
gen beta2_max95=beta2-se_beta2

gen beta3_min95=beta3+se_beta3
gen beta3_max95=beta3-se_beta3

gen cons_min95=cons+se_cons
gen cons_max95=cons-se_cons

save MI_BETAS_EQ5.dta, replace

****** SUPPLEMENTARY FIGURE S5 **************************************************************
*Regression Coefficients for the Polls (US, UK and Germany)

use MI_BETAS_EQ1.dta, clear 
drop se_beta1
drop  cons* se_cons
rename beta* eq1_beta*
sort daysbeforeED
save MI_BETAS_EQ1.dta, replace

use MI_BETAS_EQ5.dta, clear 
drop se_beta*
drop  cons* se_cons
rename beta* eq5_beta*
sort daysbeforeED
save MI_BETAS_EQ5.dta, replace

clear
use MI_BETAS_EQ1.dta, clear 
merge daysbeforeED using MI_BETAS_EQ5.dta
drop _merge 
sort daysbeforeED

* 
twoway /*
*/  || line eq1_beta1 daysbeforeED,                      /*
*/  clpattern(solid) clcolor(black) clwidth(medthick)    /* 
*/  || line eq5_beta1 daysbeforeED,                      /*
*/  clpattern(dash) clcolor(black) clwidth(medthick)     /* 
*/  || line eq1_beta1_min95 daysbeforeED,                /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line eq1_beta1_max95 daysbeforeED,                /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line eq5_beta1_min95 daysbeforeED,                /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  || line eq5_beta1_max95 daysbeforeED,                /*
*/  clpattern(dot) clcolor(black) clwidth(medium)        /*
*/  , scheme(s2mono) graphregion(color(white))           /*
*/  xtitle("Days Until Election")                        /*
*/  title("")                                            /*
*/  ylabel(0(.1)1, gmax angle(horizontal))               /*
*/  ytick(0(.1)1)                                        /*
*/  ytitle(Beta)                                         /*
*/  xlabel(0(50)200)                                     /*
*/  xscale(reverse) xscale(titlegap(2))                  /*
*/  legend(order(1 "Vote = Polls" 2 "Vote = Polls + Macro-Partisanship + Economic Perceptions") /*
*/  rows(2)                                              /*
*/  size(medsmall))

graph export FigS5.tif
